package cn.itsource.itsource.controller;

import cn.itsource.itsource.util.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.itsource.util.FastdfsUtil;

import javax.swing.plaf.multi.MultiFileChooserUI;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("/file")
public class FileController {
    public static final String FASTDFS_READ_URL = "http://47.95.117.210:8888/";
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        //调用工具类 传递文件的字节数组和后缀
        String originalFilename = file.getOriginalFilename();
        String suffix = StringUtils.substringAfterLast(originalFilename,".");

        String upload = FastdfsUtil.upload(file.getBytes(), suffix);

        //这里需要进行一下修改 返回前端时需要文件的完整地址
        HashMap<String,String> map=new HashMap<>();
        map.put("upload",upload);
        map.put("uploadUrl","http://47.95.117.210:8888"+upload);

        return AjaxResult.success("上传成功").setData(map);
    }



}
